package cn.stylefeng.guns.modular.business.frequency.orderabnormal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.entity.OrderAbnormal;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.mapper.OrderAbnormalMapper;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.param.OrderAbnormalDTO;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.param.OrderAbnormalParam;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.result.PkgOrderAbnormal;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.service.OrderAbnormalService;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.enums.OrderAbnormalExceptionEnum;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单异常表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class OrderAbnormalServiceImpl extends ServiceImpl<OrderAbnormalMapper, OrderAbnormal> implements OrderAbnormalService {

    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public PageResult<OrderAbnormal> page(OrderAbnormalParam orderAbnormalParam) {

        // 构造条件
        LambdaQueryWrapper<OrderAbnormal> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderAbnormal> list(OrderAbnormalParam orderAbnormalParam) {

        // 构造条件
        LambdaQueryWrapper<OrderAbnormal> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderAbnormalParam orderAbnormalParam) {

        // 将dto转为实体
        OrderAbnormal orderAbnormal = new OrderAbnormal();
        BeanUtil.copyProperties(orderAbnormalParam, orderAbnormal);


        this.save(orderAbnormal);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderAbnormalParam orderAbnormalParam) {

        // 根据id查询实体
        OrderAbnormal orderAbnormal = this.queryOrderAbnormal(orderAbnormalParam);


        // 更新实体
        this.updateById(orderAbnormal);

    }

    @Override
    public void edit(OrderAbnormalParam orderAbnormalParam) {

        // 根据id查询实体
        OrderAbnormal orderAbnormal = this.queryOrderAbnormal(orderAbnormalParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderAbnormalParam, orderAbnormal);

        // 不能修改状态，用修改状态接口修改状态
        orderAbnormal.setStatus(null);

        this.updateById(orderAbnormal);
    }

    @Override
    public OrderAbnormal detail(OrderAbnormalParam orderAbnormalParam) {
        return this.queryOrderAbnormal(orderAbnormalParam);
    }

    @Override
    public void changeStatus(OrderAbnormalParam orderAbnormalParam) {
        String id = orderAbnormalParam.getId();
        Integer status = orderAbnormalParam.getStatus();


        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<OrderAbnormal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(OrderAbnormal::getId, id)
                .set(OrderAbnormal::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public ResponseData getWaitList(OrderAbnormalDTO orderAbnormalDTO) {
        Page<PkgOrderAbnormal> page = this.baseMapper.getOrderAbnormalList(PageFactory.defaultPage(), orderAbnormalDTO, businessUtil.getTenantId());
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(page, orderAbnormalDTO));
    }

    @Override
    public ResponseData getRelieveListPage(OrderAbnormalDTO orderAbnormalDTO) {
        Page<PkgOrderAbnormal> page =  this.baseMapper.getOrderAbnormalList(PageFactory.defaultPage(), orderAbnormalDTO,businessUtil.getTenantId());
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(page, orderAbnormalDTO));
    }

    @Override
    public void updatePunishStatus(String id) {
        OrderAbnormal oa = this.baseMapper.selectById(id);
        oa.setPunish(oa.getPunish() == 1 ? 0 : 1);
        this.baseMapper.updateById(oa);
    }

    @Override
    public void relieve(String id) {
        this.baseMapper.relieve(id, 2, LoginContextHolder.me().getSysLoginUserId());
    }


    /**
     * 获取订单异常表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private OrderAbnormal queryOrderAbnormal(OrderAbnormalParam orderAbnormalParam) {
        OrderAbnormal orderAbnormal = this.getById(orderAbnormalParam.getId());
        if (ObjectUtil.isEmpty(orderAbnormal)) {
            throw new ServiceException(OrderAbnormalExceptionEnum.NOT_EXISTED);
        }
        return orderAbnormal;
    }
}